﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;

using CreativeMinds.StopForumSpam.HTTP;
using CreativeMinds.StopForumSpam.Responses;

namespace CreativeMinds.StopForumSpam {

	public class Client : HTTPClient {
		private readonly String apiKey;
		private readonly String format;

		public Client() : this(String.Empty) { }
		public Client(String apiKey) {
			this.apiKey = apiKey;
			this.format = "json";
		}

		public Response CheckEmailAddress(String emailAddress) {
			return this.Check(new { email = emailAddress });
		}

		public Response CheckUsername(String username) {
			return this.Check(new { username = username });
		}

		public Response CheckIPAddress(String ipAddress) {
			IPAddress address;
			if (!IPAddress.TryParse(ipAddress, out address)) {
				throw new ArgumentException("The ipAddress argument is not a valid IP address.");
			}
			return this.Check(new { ip = address.ToString() });
		}

		public Response CheckIPAddress(IPAddress ipAddress) {
			return this.Check(new { ip = ipAddress });
		}

		public Response Check(String username, String emailAddress, String ipAddress) {
			IPAddress address = null;
			if (!String.IsNullOrWhiteSpace(ipAddress) &&  !IPAddress.TryParse(ipAddress, out address)) {
				throw new ArgumentException("The ipAddress argument is not a valid IP address.");
			}
			return this.Check(username, emailAddress, address);
		}

		public Response Check(String username, String emailAddress, IPAddress ipAddress) {
			Dictionary<String, String> parameters = new Dictionary<String, String>();
			if (!String.IsNullOrWhiteSpace(username)) {
				parameters.Add("username", username);
			}
			if (!String.IsNullOrWhiteSpace(emailAddress)) {
				parameters.Add("email", emailAddress);
			}
			if (ipAddress != null) {
				parameters.Add("ip", ipAddress.ToString());
			}
			return this.Check(parameters);
		}

		private Response Check(Object values) {
			Dictionary<String, String> parameters = new Dictionary<String, String>();
			if (values != null) {
				foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(values)) {
					parameters.Add(property.Name, property.GetValue(values).ToString());
				}
			}
			return this.Check(parameters);
		}

		private Response Check(Dictionary<String, String> parameters) {
			parameters.Add("f", this.format);
			return Response.Parse(this.Get(new Uri("http://www.stopforumspam.com/api"), parameters), this.format);
		}

		public Response AddSpammer(String username, String emailAddress, String ipAddress) {
			IPAddress address;
			if (IPAddress.TryParse(ipAddress, out address)) {
				return this.AddSpammer(username, emailAddress, address);
			}
			throw new ArgumentException("The ipAddress argument is not a valid IP address.");
		}

		public Response AddSpammer(String username, String emailAddress, IPAddress ipAddress) {
			if (String.IsNullOrWhiteSpace(this.apiKey)) {
				throw new ArgumentNullException("apiKey");
			}
			if (String.IsNullOrWhiteSpace(username)) {
				throw new ArgumentNullException("username");
			}
			if (String.IsNullOrWhiteSpace(emailAddress)) {
				throw new ArgumentNullException("emailAddress");
			}

			Dictionary<String, String> parameters = new Dictionary<String,String>();
			parameters.Add("username", username);
			parameters.Add("ip_addr", username);
			parameters.Add("email", emailAddress);
			parameters.Add("api_key", this.apiKey);

			return Response.Parse(this.Post(new Uri("http://www.stopforumspam.com/add.php"), parameters), this.format);
		}
	}
}